//This test verified how much home loan a customer can borrow based on parameters input.

package practice;


import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

public class HomeloanCalculator {
	private WebDriver driver;
	private String baseUrl;
	private StringBuffer verificationErrors = new StringBuffer();
	@Before
	public void setUp() throws Exception {
		driver = new ChromeDriver();
		baseUrl = "http://hlc1.westpac.com.au/";
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
	}

	@Test
	public void testWestpacLoanContinued() throws Exception {
		driver.get(baseUrl + "/hlc/hlc/BorrowingPowerStart.do");
		new Select(driver.findElement(By.name("applicants"))).selectByVisibleText("Myself and my spouse/defacto");
		new Select(driver.findElement(By.name("numDependants"))).selectByVisibleText("2");
		driver.findElement(By.name("Continue")).click();
		driver.findElement(By.name("income1")).clear();
		driver.findElement(By.name("income1")).sendKeys("10000");
		driver.findElement(By.name("income2")).clear();
		driver.findElement(By.name("income2")).sendKeys("1000");
		//new Select(driver.findElement(By.id("fieldMediumSelect"))).selectByVisibleText("Married/defacto");
		//new Select(driver.findElement(By.name("maritalStatus2"))).selectByVisibleText("Married/defacto");
		
		driver.findElement(By.name("calculate")).click();
		
		
		//Get value of the loan power
		//System.out.println(driver.findElement(By.id("redAnswer")).getText());
		
		String LoanPower = driver.findElement(By.id("redAnswer")).getText();
		String[] Values = LoanPower.split(" ");
		//System.out.println(Values[5]);
		
		//Insert check point - check if the loan power is as expected
		if(Values[5].equals("$1021263")){System.out.println("Passed");}
		else
		{System.out.println("Failed");}
		
		
		
		//Another way to close window even there is no close button
		//String WindowHandle = driver.getWindowHandle();
		//driver.switchTo().window(WindowHandle).close();
		driver.findElement(By.linkText("Close window")).click();
		
		
		
	}

	@After
	public void tearDown() throws Exception {
		//driver.quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			fail(verificationErrorString);
		}
	}
}

